package com.nowcoder.community.controller;
import com.nowcoder.community.entity.Event;
import com.nowcoder.community.entity.Page;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.event.EventProducer;
import com.nowcoder.community.service.FollowService;
import com.nowcoder.community.service.UserService;
import com.nowcoder.community.util.HostHolder;
import com.nowcoder.community.util.XaCommunityConstant;
import com.nowcoder.community.util.XaCommunityUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static com.nowcoder.community.util.XaCommunityConstant.*;
@Controller
public class FollowController {
    @Resource
    private FollowService followService;
    @Resource
    private HostHolder hostHolder;
    @Resource
    private UserService userService;
    @Resource
    private EventProducer eventProducer;
    private boolean hasFollowed(int userId) {
        if (hostHolder.getUser() == null)  return false;
        return followService.hasFollowed(hostHolder.getUser().getId(), ENTITY_TYPE_USER, userId);
    }
    @PostMapping("/follow")
    @ResponseBody
    public String follow(int entityType,int entityId){
        User user=hostHolder.getUser();
        followService.follow(user.getId(),entityType,entityId);
        //5.触发点赞事件
        Event event = new Event()
            .setTopic(TOPIC_FOLLOW)
            .setUserId(user.getId())
            .setEntityId(entityId)
            .setEntityType(entityType)
            .setEntityUserId(entityId);
        
        eventProducer.fireEvent(event) ;
        return XaCommunityUtil.getJSONString(0,"已关注");
    }
    //1.关注
    @PostMapping("/unfollow")
    @ResponseBody
    public String unfollow(int entityType,int entityId){
        User user=hostHolder.getUser();

        followService.unFollow(user.getId(),entityType,entityId);
        return XaCommunityUtil.getJSONString(0,"已取消关注");
    }
    //2.取消关注
    @GetMapping("/followees/{userId}")
    public String getFollowees(@PathVariable("userId")int userId, Page page, Model model){
        User user= userService.findUserById(userId);
        if(user==null){
            throw new RuntimeException("该用户不存在");
        }
        //添加查询用户到页面模型
        model.addAttribute("user",user);
        
        //设置分页
        page.setLimit(5);
        page.setPath("/followees/"+userId);
        page.setRows((int) followService.findFolloweeCount(userId,
                ENTITY_TYPE_USER));

        List<Map<String,Object>>userList=followService.findFollowees(
                userId,page.getOffset(),page.getLimit());
        
        //只是顺便给每个user对象添加”是否关注“的属性
        if(userList!=null){
            for(Map<String,Object> map:userList){
                User u=(User) map.get("user");
                map.put("hasFollowed",hasFollowed(u.getId()));
            }
        }
        model.addAttribute("users",userList);
        return "/site/followee";
    }
    //3.获取关注的博主列表
    @GetMapping("/followers/{userId}")
    public String getFollowers(@PathVariable("userId")int userId, Page page, Model model){
        User user= userService.findUserById(userId);
        if(user==null){
            throw new RuntimeException("该用户不存在");
        }
        //添加查询用户到页面模型
        model.addAttribute("user",user);

        //设置分页
        page.setLimit(5);
        page.setPath("/followers/"+userId);
        page.setRows((int) followService.findFolloweeCount(userId,
                ENTITY_TYPE_USER));

        List<Map<String,Object>>userList=followService.findFollowers(
                userId,page.getOffset(),page.getLimit());

        //只是顺便给每个user对象添加”是否关注“的属性
        if(userList!=null){
            for(Map<String,Object> map:userList){
                User u=(User) map.get("user");
                map.put("hasFollowed",hasFollowed(u.getId()));
            }
        }
        model.addAttribute("users",userList);
        return "/site/follower";
    } 
    //4.获取博主的粉丝列表
    
    
}
